var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
var wxAuthFail = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    util:util,
    tc: 0,
    uid:0,
    token: '',
    openid: '',
    english: [
      { key: 'A', value: 'A',},
      { key: 'B', value: 'B' },
      { key: 'C', value: 'C' },
      { key: 'D', value: 'D' },
      { key: 'E', value: 'E' },
      { key: 'F', value: 'F' },
      { key: 'G', value: 'G' },
      { key: 'H', value: 'H' },
      { key: 'I', value: 'I' },
      { key: 'J', value: 'J' },
      { key: 'K', value: 'K' },
      { key: 'L', value: 'L' },
      { key: 'M', value: 'M' },
      { key: 'N', value: 'N' },
      { key: 'O', value: 'O' },
      { key: 'P', value: 'P' },
      { key: 'Q', value: 'Q' },
      { key: 'R', value: 'R' },
      { key: 'S', value: 'S' },
      { key: 'T', value: 'T' },
      { key: 'U', value: 'U' },
      { key: 'V', value: 'V' },
      { key: 'W', value: 'W' },
      { key: 'X', value: 'X' },
      { key: 'Y', value: 'Y' },
      { key: 'Z', value: 'Z' },
      ],

      artArray:[],
      artArrayLinshi:[],

      artItem_w: 55,

    noData:false,
    isIphone_x:false,
    scrollView_h: 480
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var tc = app.get_tc();
    var that = this;
    that.setData({
      tc: tc,
    });
  

    var titlename = tc == 1 ? '藝術家' : '艺术家';
    
    wx.setNavigationBarTitle({
      title: titlename,
    }); 

     wx.showLoading({
        title: tc == 1 ? '加載中' : '加载中',
        mask: 'true'
      });

    wx.getSystemInfo({
      success: function(res) {
        var isIphone_x = (res.model.indexOf('iPhone X') >= 0);
        that.setData({
          isIphone_x: isIphone_x,
          scrollView_h: isIphone_x ? (res.windowHeight - 70) : (res.windowHeight - 50),
          artItem_w: (res.windowWidth - 70) / 4
        })
      },
    })

     //获取颜色配置
    app.getColorSetData(function (color) {
      console.log("color=====",color);
      that.setData({
        colorSet: color,
      })
    });

    //初始化底部导航
    var b_nav_data = app.getBottomNavData('artist/artist', function (data) {
      that.setData({
        bottom_nav_data: data,
        nav_bgColor: data[0].bgcolor
      });
    });

    that.getLoginInfo();
  
    that.getYishujia();
  },

  getLoginInfo: function () {
    var that = this;
    app.LoginUserInfo(function (res) {
      console.log("mine login:", res);
      wxAuthFail = res == -1;//用户拒绝授权
      if (wxAuthFail) {
        return;
      }
      that.setData({
        uid: res.uid,
        openid: res.wxsmall_openid,
        token: res.token
      });
    });
  },

  //获取艺术家
  getYishujia:function(){
    var that = this;
    util.api({
      data: {
        "publickey": util.publiccode,
        "ac": "getYishujia",
        //"tc": that.data.tc
      },
      success: function (responseData) {
        console.log("getYishujia===", responseData);
        if(responseData.data.res == false){
          that.setData({
            noData: true
          })
        }else{
          let no_data = responseData.data.length <= 0;
          that.setData({
            artArray:responseData.data,
            artArrayLinshi:responseData.data,
            noData: no_data
          })
          console.log("artArrayLinshi2===", that.data.artArrayLinshi);
        }
         wx.hideLoading();
      }
    });
  },

  //点击英文字母搜索对应的艺术家
  searchTap:function(e){
    var that = this;
    console.log("searchTap===", e);
    var key = e.currentTarget.dataset.key;
    
    var result = that.data.artArray;
    console.log("searchTap===", result[key]);
    if(result[key] != undefined){
      var newArray = {};
      newArray[key] = result[key];
      that.setData({
        artArrayLinshi:newArray
      })
    }else{
      that.setData({
        artArrayLinshi:[]
      })
    }
    
    console.log("artArrayLinshi===", that.data.artArrayLinshi);
  },

  goAtherPage: function (e) {
    var url = e.currentTarget.dataset.link;
    if (url) {
      wx.navigateTo({ url: url });
    }
  },

  //保存formId
  submitFormId: function (e) {
    app.saveFormId(e.detail.formId);

    var path = e.detail.value.nav_path;
    if (path) {
      wx.redirectTo({ url: path })
    }
  },
})